﻿package framework.core;

import java.util.*;

import com.hitrust.gs.service.GSBaseBean;

/**
 * Creation date: (2003-08-28)
 * @author: adminnistrator
 * 对象集合类
 */

public class ObjectList 
{
	public static final int ALL = Value.NULL_INT ;

	private Vector m_objs = new Vector();
	private int    m_page_size = ALL ;
	private int    m_page_num = 1 ;
	private int    m_total_size = -1 ;
	private int    m_total_pages = 1 ;

	//禁止直接调用构造函数
	private ObjectList()
	{
	}
	public static ObjectList getList()
	{
		ObjectList list = new ObjectList();
		list.m_page_size = 0 ; 
		list.m_page_num = 0 ;
		list.m_total_size = 0;
		list.m_total_pages = 0 ;
		return list;
	}
	public static ObjectList fromRs(Class cls,java.sql.ResultSet rs) throws Exception
	{
		return fromRs(cls,rs,ALL,1);
	}
	public static ObjectList fromRs(Class cls,java.sql.ResultSet rs,int page_size) throws Exception
	{
		return fromRs(cls,rs,page_size,1);
	}
	public static ObjectList fromRs(Class cls,java.sql.ResultSet rs,int page_size,int page_num) throws Exception
	{
		return fromRs(cls,rs,page_size,page_num,-1);
	}
	public static ObjectList fromRs(Class cls,java.sql.ResultSet rs,int page_size,int page_num,int total) throws Exception
	{
		ObjectList list = new ObjectList();
        page_num = getPageNum(page_size, page_num, total);		
		int count = 0;
		int start = page_size * (page_num - 1) + 1;
		int end = start + page_size;

		if (ALL == page_size) 
		{
			start = 0;
			end = Integer.MAX_VALUE;
			page_num = 1;
		}

		if(total>0) //优化大的记录集
		{
			while (rs.next()) 
			{
				count++;
				if (count < start) continue;
				if (count >= end) break;
				DBData t = (DBData)cls.newInstance();
				t.initObject(rs);	
				list.m_objs.add(t);
			}
			count = total;
		} 
		else 
		{
			while (rs.next()) 
			{
				count++;
				if (count < start || count >= end) continue;
				DBData t = (DBData)cls.newInstance();
				t.initObject(rs);	
				list.m_objs.add(t);
			}
		}

		/*
		list.m_total_size = count;
		list.m_page_size = page_size;
		list.m_page_num = page_num;
		int yu = list.m_total_size % list.m_page_size;
		int shang = list.m_total_size / list.m_page_size;
		list.m_total_pages = (0 == yu) ? shang : shang + 1;
		if (list.m_total_pages == 0) list.m_page_num = 0;
		*/
        list.m_total_size = count;
        list.m_page_size = page_size;
        list.m_page_num = page_num;
        int yu = list.m_total_size % list.m_page_size;
        int shang = list.m_total_size / list.m_page_size;
        list.m_total_pages = (0 == yu) ? shang : shang + 1;
        if (list.m_total_pages == 0) list.m_page_num = 0;
//        if (yu == 0 && list.m_page_num > 1) list.m_page_num = page_num - 1;		
		return list;
	}
	
	public static ObjectList fromRsMini(Class cls, java.sql.ResultSet rs, int page_size, int page_num, int total) throws Exception {
        ObjectList list = new ObjectList();
        page_num = getPageNum(page_size, page_num, total);
        int count = 0;

        while (rs.next()) {
            count++;
            DBData t = (DBData) cls.newInstance();
            t.initObject(rs);
            list.m_objs.add(t);
        }

        list.m_total_size = total;
        list.m_page_size = page_size;
        list.m_page_num = page_num;
        int yu = 0;
        int shang = 0;
        if(list.m_page_size > 0) {
            yu =list.m_total_size % list.m_page_size;
            shang = list.m_total_size / list.m_page_size;
        }
        list.m_total_pages = (0 == yu) ? shang : shang + 1;
        if (list.m_total_pages == 0)
            list.m_page_num = 0;
        return list;
    }
	
	public static ObjectList fromRsMiniByProcess(GSBaseBean aBea, java.sql.ResultSet rs, int page_size, int page_num, int total) throws Exception {
        ObjectList list = new ObjectList();
        page_num = getPageNum(page_size, page_num, total);
        int count = 0;

        while (rs.next()) {
            count++;
            list.m_objs.add(aBea.process(rs));
        }

        list.m_total_size = total;
        list.m_page_size = page_size;
        list.m_page_num = page_num;
        int yu = 0;
        int shang = 0;
        if(list.m_page_size > 0) {
            yu =list.m_total_size % list.m_page_size;
            shang = list.m_total_size / list.m_page_size;
        }
        list.m_total_pages = (0 == yu) ? shang : shang + 1;
        if (list.m_total_pages == 0)
            list.m_page_num = 0;
        return list;
    }
	
	public static int getTotalPages(int page_size, int total){
		 int yu = 0;
	     int shang = 0;
	     if(page_size>0){
	    	  yu =total % page_size;
	          shang = total / page_size;
	     }
	     return (0 == yu) ? shang : shang + 1;
	}
	
	public static int getPageNum(int page_size, int page_num, int total) {
		if (total > 0 && page_num > (total / page_size)) {
            if (total % page_size == 0)
                page_num = (total / page_size);
            else
                page_num = (total / page_size) + 1;
        }
		return page_num;
	}

    public static ObjectList fromRsMini(java.sql.ResultSet rs, int page_size, int page_num, int total) throws Exception {
        ObjectList list = new ObjectList();
        page_num = getPageNum(page_size, page_num, total);
        int count = 0;

        while (rs.next()) {
            count++;
            UniversalRow ur = new UniversalRow();
            ur.initObject(rs);
            list.m_objs.add(ur);
        }

        list.m_total_size = total;
        list.m_page_size = page_size;
        list.m_page_num = page_num;
        int yu = 0;
        int shang = 0;
        if(list.m_page_size > 0) {
            yu =list.m_total_size % list.m_page_size;
            shang = list.m_total_size / list.m_page_size;
        }
        list.m_total_pages = (0 == yu) ? shang : shang + 1;
        if (list.m_total_pages == 0)
            list.m_page_num = 0;
        return list;
    }
	
	public static ObjectList fromRs(java.sql.ResultSet rs) throws Exception
	{
		return fromRs(rs, ALL);
	}
	public static ObjectList fromRs(java.sql.ResultSet rs,int page_size) throws Exception
	{
		return fromRs(rs, page_size, 1);
	}
	public static ObjectList fromRs(java.sql.ResultSet rs,int page_size,int page_num) throws Exception
	{
		return fromRs(rs, page_size, page_num, -1);
	}
	public static ObjectList fromRs(java.sql.ResultSet rs,int page_size,int page_num,int total) throws Exception
	{
		ObjectList list = new ObjectList();
		int count = 0;
		int start = page_size * (page_num - 1) + 1;
		int end = start + page_size;

		if (ALL == page_size) 
		{
			start = 0;
			end = Integer.MAX_VALUE;
			page_num = 1;
		}

		if(total>0) //优化大的记录集
		{
			while (rs.next()) 
			{
				count++;
				if (count < start) continue;
				if (count >= end) break;
				//DBData t = (DBData)cls.newInstance();
				UniversalRow ur = new UniversalRow();
				ur.initObject(rs);	
				list.m_objs.add(ur);
			}
			count = total;
		} 
		else 
		{
			while (rs.next()) 
			{
				count++;
				if (count < start || count >= end) continue;
				UniversalRow ur = new UniversalRow();
				ur.initObject(rs);	
				list.m_objs.add(ur);
			}
		}

		list.m_total_size = count;
		list.m_page_size = page_size;
		list.m_page_num = page_num;
		int yu = list.m_total_size % list.m_page_size;
		int shang = list.m_total_size / list.m_page_size;
		list.m_total_pages = (0 == yu) ? shang : shang + 1;
		if (list.m_total_pages == 0) list.m_page_num = 0;
		return list;
	}	
	
	public Object get(int i) throws Exception
	{
		if(i>size()-1)
		{
			Logger.throwException("ObjectList.get()","超出列表范围:size()="+size()+",i="+i+"!");
		}
		return m_objs.get(i);
	}
	public int getPageNum()
	{
		return m_page_num;
	}
	public int getPageSize()
	{
		return m_page_size ;
	}
	public int getTotalPage()
	{
		return m_total_pages;
	}
	public int getTotalSize()
	{
		return m_total_size;
	}
	public int size()
	{
		return m_objs.size();
	}
	
	public void add(Object v)
	{
		m_objs.add(v);
	}
	public void remove(int index)
	{
		m_objs.remove(index);
	}
	public void set(int index,Object element)
	{
		m_objs.set(index,element);
	}
	public void setPageSize(int mPageSize) {
		m_page_size = mPageSize;
	}
	public void setPageNum(int mPageNum) {
		m_page_num = mPageNum;
	}
	public void setTotalSize(int mTotalSize) {
		m_total_size = mTotalSize;
	}
	public void setTotalPages(int mTotalPages) {
		m_total_pages = mTotalPages;
	}
	
	
}
